package com.besttone.action;

import com.besttone.bean.SignallingBean;
import com.besttone.enums.EnumAPIResultCode;
import com.besttone.util.MD5Util;
import com.besttone.util.PropertieUtils;
import com.besttone.util.SolrCommonUtil;
import com.besttone.util.SolrObject;
import com.jfinal.core.Controller;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.common.SolrDocumentList;

import java.util.List;

/**
 * 根据日期、主叫、被叫查询信令详情
 * Created by zhengzhenzhen on 2017/9/5.
 * http://localhost:9090/signalAndsms_2_solr/signalling?day_id_from=20170905&day_id_to=20170906&province=330000&calling=057188810736
 */
public class SignallingAction extends Controller {
    private static Log _log = LogFactory.getLog(SecurityFlashSmsAction.class);

    @SuppressWarnings("rawtypes")
    public void index() {
        String streaming_no = getPara("streaming_no");
        String sp_account = getPara("sp_account");
        String sp_pwd = getPara("sp_pwd");//密码：MD5(streaming_no&sp_account&"flashsms")
        String province=getPara("province");
        String calling = getPara("calling");// 主叫
        String called = getPara("called");// 被叫
        String day_id_from = getPara("day_id_from");// 发送时间开始：按天
        String day_id_to = getPara("day_id_to");// 发送时间结束：按天
        String current_page = StringUtils.isBlank(getPara("current_page")) ? "1"
                : getPara("current_page");// 当前页数
        String per_page_size = StringUtils.isBlank(getPara("per_page_size")) ? "10"
                : getPara("per_page_size");// 每页条数，默认10
        StringBuffer pwdBuf=new StringBuffer();
        String nowpwd= MD5Util.md5(pwdBuf.append(streaming_no).append("&").append(sp_account)
                .append("&").append("flashsms").toString());//加密方式
        _log.info("请求查询参数:streaming_no=" + streaming_no + ",sp_account="
                + sp_account + ",calling=" + calling
                + ",called=" + called + ",per_page_size="
                + per_page_size + ",sp_pwd="+sp_pwd+",nowpwd="+nowpwd+",day_id_from="+day_id_from+
                ",day_id_to="+day_id_to+",province="+province);
        /**if(!nowpwd.equals(sendPwd)){
         setAttr("result_code",
         EnumAPIResultCode.UNAUTHORIZED_BUSINESS.getValue());
         setAttr("result_msg", "鉴权失败:密码错误");
         renderJson();
         return;
         }
         if (StringUtils.isBlank(sp_account)) {
         setAttr("result_code",
         EnumAPIResultCode.PARAMS_INCOMPLETE.getValue());
         setAttr("result_msg", "账号不能为空");//账号为空会躲避密码验证
         renderJson();
         return;
         }**/
         if (StringUtils.isBlank(day_id_from)) {
         setAttr("result_code",
         EnumAPIResultCode.PARAMS_INCOMPLETE.getValue());
         setAttr("result_msg", "开始日期不能为空");//
         renderJson();
         return;
         }
        if (StringUtils.isBlank(day_id_to)) {
            setAttr("result_code",
                    EnumAPIResultCode.PARAMS_INCOMPLETE.getValue());
            setAttr("result_msg", "结束日期不能为空");//
            renderJson();
            return;
        }
        if (StringUtils.isBlank(province)) {
            setAttr("result_code",
                    EnumAPIResultCode.PARAMS_INCOMPLETE.getValue());
            setAttr("result_msg", "省份编码不能为空");//
            renderJson();
            return;
        }
        //开始结束日期必填,格式:20170905
        StringBuffer strBuf = new StringBuffer();
        strBuf.append(" day_id:[")
                .append(day_id_from)
                .append(" TO ")
                .append(day_id_to)
                .append("]")
                .append(" AND (province:")////省份必填
                .append(province)
                .append(") ");
        //省份必填
        /**if (StringUtils.isNotBlank(province)) {
            province = StringUtils.trim(province);
            strBuf.append(" AND (province:");
            strBuf.append(province);
            strBuf.append(") ");
        }**/
        if (StringUtils.isNotBlank(calling)) {
            calling = StringUtils.trim(calling);
            strBuf.append(" AND calling:");
            strBuf.append(calling);
        }
        //strBuf.append("called_number:"+called_number);//default
        if (StringUtils.isNotBlank(called)) {
            called = StringUtils.trim(called);
            strBuf.append(" AND called:");
            strBuf.append(called);
        }
        String querySql = strBuf.toString();
        _log.info("solr查询语句:" + querySql);
        try {
            setAttr("streaming_no", streaming_no);
            SolrDocumentList docList = SolrCommonUtil.search(PropertieUtils
                            .getProperty("collectionSignalling"),querySql,"id,province,calling,called,createtime,day_id","day_id", SolrQuery.ORDER.desc,
                    Integer.valueOf(current_page),
                    Integer.valueOf(per_page_size));
            setAttr("result_code", EnumAPIResultCode.SUCCESS.getValue());
            List list = SolrObject.toBeanList(docList,
                    SignallingBean.class);
            setAttr("result_msg", list);// 返回详情数据
            setAttr("result_num", docList.getNumFound());
        } catch (Exception e) {
            _log.info("solr查询异常:" + e);
            setAttr("result_code", EnumAPIResultCode.SYSTEM_ERROR.getValue());
            setAttr("result_msg", EnumAPIResultCode.SYSTEM_ERROR.getName());
        }
        renderJson();
        return;
    }
}
